package com.qf.admin.controller;

import com.qf.admin.entity.dto.Admin;
import com.qf.admin.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class LoignController {

    @Autowired
    private AdminService adminService;

    @RequestMapping({"login" , "/"})
    public String toLogin(){
        return "login";
    }

    @RequestMapping("login.do")
    public String login(@RequestParam(value = "username" , required = true) String username ,
                        @RequestParam(value = "password" , required = true) String password ,
                        Model model){
        Admin admin = adminService.getByUsername(username);
        if (admin == null){
            model.addAttribute("error" , "用户名或密码错误！");
            return "forward:/";

        }
        if (!password.equals(admin.getPassword())){
            model.addAttribute("error" , "用户名或密码错误！");
            return "forward:/";
        }
        return "redirect:index";
    }


}
